﻿namespace KeRui.Upload
{
    using System;
    using System.IO;

    internal class MacBinary
    {
        private byte mAttr1;
        private byte mAttr2;
        private int mDataFork;
        private int mDataLen;
        private SuperStream mDataStream;
        private uint mMacCreator;
        private uint mMacType;
        private string mMIMEType;
        private int mPadding;
        private int mRezFork;
        private int mRezLen;
        private SuperStream mRezStream;
        private SuperStream mStream;

        public MacBinary()
        {
            this.mStream = this.mDataStream = (SuperStream) (this.mRezStream = null);
            this.mMIMEType = null;
            this.mDataFork = this.mRezFork = this.mDataLen = this.mRezLen = -1;
            this.mAttr1 = (byte) (this.mAttr2 = 0);
            this.mMacType = this.mMacCreator = 0;
        }

        private int FindRezFork()
        {
            RezFork fork = new RezFork();
            int num = 0;
            int num2 = 0;
            int inStart = (this.mDataFork + this.mDataLen) + this.mPadding;
            fork.SetData(new SuperStream(this.mStream, inStart, this.mRezLen));
            if (fork.IsOK())
            {
                return inStart;
            }
            inStart = this.mDataFork + this.mDataLen;
            num2 = this.mPadding + 0x80;
            if (num2 > ((this.Length - 0x80) - this.mDataLen))
            {
                num2 = (this.Length - 0x80) - this.mDataLen;
            }
            for (num = 1; num <= num2; num++)
            {
                fork.SetData(new SuperStream(this.mStream, inStart, this.mRezLen));
                if (fork.IsOK())
                {
                    return inStart;
                }
                inStart++;
            }
            return 0;
        }

        private SuperStream GetDataStream()
        {
            int mDataFork = this.mDataFork;
            int mDataLen = this.mDataLen;
            if ((mDataFork < 0) || (mDataLen < 0))
            {
                mDataFork = mDataLen = 0;
            }
            return new SuperStream(this.mStream, mDataFork, mDataLen);
        }

        public string GetFileXt()
        {
            return MacFileType.InferFileType(this.mMacType, this.mMacCreator);
        }

        public int GetRezFork()
        {
            if (this.mRezFork < 0)
            {
                this.mRezFork = this.FindRezFork();
            }
            return this.mRezFork;
        }

        private SuperStream GetRezStream()
        {
            int rezFork = this.GetRezFork();
            int mRezLen = this.mRezLen;
            if ((rezFork < 0) || (mRezLen < 0))
            {
                rezFork = mRezLen = 0;
            }
            return new SuperStream(this.mStream, rezFork, mRezLen);
        }

        public bool IsOK()
        {
            if (((this.mMIMEType != "application/x-macbinary") && (this.mMIMEType != "")) && (this.mMIMEType != null))
            {
                return false;
            }
            if (this.mStream == null)
            {
                return false;
            }
            if (this.Length < 0x80)
            {
                return false;
            }
            if (this.mDataLen < 0)
            {
                return false;
            }
            if (this.mRezLen < 0)
            {
                return false;
            }
            if ((this.mDataFork + this.mDataLen) > this.Length)
            {
                return false;
            }
            int rezFork = this.GetRezFork();
            if (rezFork == 0)
            {
                return false;
            }
            if ((rezFork + this.mRezLen) > this.Length)
            {
                return false;
            }
            return true;
        }

        public void SetData(SuperStream inStream, string inMIMEType)
        {
            this.mStream = inStream;
            this.mDataStream = (SuperStream) (this.mRezStream = null);
            this.mMIMEType = inMIMEType;
            if (this.mStream.Length >= 0x80L)
            {
                this.mDataFork = 0x80;
                this.mRezFork = -1;
                this.mStream.Position = 0x53L;
                this.mDataLen = this.mStream.GetMacLONG();
                this.mStream.Position = 0x57L;
                this.mRezLen = this.mStream.GetMacLONG();
                this.mPadding = ((this.mDataLen % 0x80) != 0) ? (0x80 - (this.mDataLen % 0x80)) : 0;
                this.mStream.Position = 0x49L;
                this.mAttr1 = this.mStream.GetBYTE();
                this.mStream.Position = 0x65L;
                this.mAttr2 = this.mStream.GetBYTE();
                this.mStream.Position = 0x41L;
                this.mMacType = this.mStream.GetOSType();
                this.mStream.Position = 0x45L;
                this.mMacCreator = this.mStream.GetOSType();
            }
            else
            {
                this.mDataFork = this.mRezFork = this.mDataLen = this.mRezLen = this.mPadding = -1;
                this.mAttr1 = (byte) (this.mAttr2 = 0);
                this.mMacType = this.mMacCreator = 0;
            }
        }

        public byte Attr1
        {
            get
            {
                return this.mAttr1;
            }
        }

        public byte Attr2
        {
            get
            {
                return this.mAttr2;
            }
        }

        public Stream DataStream
        {
            get
            {
                if (this.mDataStream == null)
                {
                    this.mDataStream = this.GetDataStream();
                }
                return this.mDataStream;
            }
        }

        public int Length
        {
            get
            {
                return (int) this.mStream.Length;
            }
        }

        public uint MacCreator
        {
            get
            {
                return this.mMacCreator;
            }
        }

        public uint MacType
        {
            get
            {
                return this.mMacType;
            }
        }

        public Stream RezStream
        {
            get
            {
                if (this.mRezStream == null)
                {
                    this.mRezStream = this.GetRezStream();
                }
                return this.mRezStream;
            }
        }
    }
}

